package com.java.al.swing;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Button2 extends JFrame{
	private JButton
		b1 = new JButton("Button 1"),
		b2 = new JButton("Button 2");
	private JTextField txt = new JTextField(10);
	class ButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			String name = ((JButton)e.getSource()).getText(); //e.getSource() 获得事件源!  ActionEvent 包含事件和事件源的所有信息!

			txt.setText(name);
		}
	}
	private ButtonListener bl = new ButtonListener();
	public Button2(){
		b1.addActionListener(bl); //.addActionListener( ActionListener )将ButtonListener注册给两个按钮!
		b2.addActionListener(bl);
		setLayout( new FlowLayout());
		add(b1);
		add(b2);
		add(txt);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingConsole.run(new Button2(), 200, 100);
	}

}
